package com.yuanfeng.job.utils;

import com.alibaba.fastjson.JSONObject;
import com.yuanfeng.job.vo.QuWxGoodsListVO;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName WxLiveUtil
 * @Description 微信审核工具类
 * @Author liulh
 * @Date 2023/5/17 11:46
 * @Version 1.0
 **/
@Component
public class WxLiveUtil {

    private static final String UPLOAD_URL = "/wxaapi/broadcast/goods/getapproved";


    public QuWxGoodsListVO queryWxLiveGoodsList(String offset, String status) throws Exception {

        String accessToken = AccessTokenUtil.queryAccessToken();
        String url = UPLOAD_URL.replace("ACCESS_TOKEN", accessToken);

        Map<String, String> map = new HashMap<>();
        map.put("offset", offset);
        map.put("status", status);
        map.put("access_token", accessToken);

        String host = "https://api.weixin.qq.com";
        String method = "GET";
        Map<String, String> headers = new HashMap<String, String>();
        String accept = headers.put("Accept", "charset=utf-8");// 格式为:Authorization:APPCODE，存放appcode

        String result = null;
        HttpResponse httpResponse = HttpUtil.doGet(host, url, method, headers, map);
        if (null != httpResponse) {
            HttpEntity entity = httpResponse.getEntity();
            if (null != entity) {
                result = EntityUtils.toString(entity, "utf-8");
            }
        }
        QuWxGoodsListVO jsonObject = JSONObject.parseObject(result, QuWxGoodsListVO.class);
////        System.out.println(jsonObject.getAccess_token());
//        System.out.println(jsonObject.getErrcode());
//        System.out.println(jsonObject.getGoods().size());
        return jsonObject;
    }

}
